package com.hyt.it.ogt.ks.mq;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.amqp.core.Message;

import com.alibaba.fastjson.JSON;
import com.hyt.it.ogt.ks.mq.model.CandidateAnswerLogBackupDTO;
import com.hyt.it.ogt.ks.service.CandidateAnswerLogService;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wuyingge
 * @ClassName CandidateAnswerLogConsumer.java
 * @Description 考生保存答案请求参数日志
 * @createTime 2021年10月16日 14:41:00
 */
//@Component
@Slf4j
public class CandidateAnswerLogConsumer {

    @Resource
    private CandidateAnswerLogService candidateAnswerLogService;

    /*@RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "${mq.candidateAnswerLog.queue}", autoDelete = "false",durable = "true"),
            exchange = @Exchange( value = "${mq.ksDirect}"),
            key = {"${mq.candidateAnswerLog.route}"})})*/
    public void initCandidateDataMessage(String json, Message message, Channel channel){
        try {
            log.info("初始化考生答案日志MQ监听消息内容:【{}】", json);
            //处理日志
            CandidateAnswerLogBackupDTO answerLogs = JSON.parseObject(json,CandidateAnswerLogBackupDTO.class);
            candidateAnswerLogService.consumerLog(answerLogs);
        } catch (Exception e) {
            log.error("初始化考生答案日志MQ异常: 【{}】------{}", json, e);
        } finally {
            try {
                // 确认消费一条消息
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                log.error("初始化考生答案日志消息异常: 【{}】------{}", json, e);
            }
        }
    }
}
